import axios from "axios";

//axios全局配置
const instance = axios.create({
    baseURL : 'http://shopapi.cn/admin',
    timeout : 5000
})
// axios请求修改器
instance.interceptors.request.use(
    request=>{
        console.log('请求修改器,这里处理token');
        return request;
    },error => {
        return Promise.reject(error);
    }
);
// axios响应修改器
instance.interceptors.response.use(
    response=>{
        console.log('响应修改器,这里处理token');
        return response;
    },error => {
        return Promise.reject(error);
    }
)
export function get(url,params){
    return instance.get(url,{
        params
    })
}
export function post(url,params){
    return instance.post(url,params,{
        transformRequest:[
            function(data){
                let str = '';
                console.log(data);
                for(let key in data){
                    console.log(key);
                    console.log(data[key]);
                    str += encodeURIComponent(key) +
                        '='
                        + encodeURIComponent(data[key]) + '&'
                }

                return str;
            }
        ],
        headers: {
            "Content-Type": "application/x-www-form-urlencoded"
        }
    })
}